package test.day02;

public class KthLargestNumberInArray {
	public int findKthLargest(int[] nums, int k) {
		if (nums.length == 0 || k < 1) {
			return -1;
		}

		// 对nums数组进行排序
		insertionSort(nums);
		System.out.println(nums[0]);
		System.out.println(nums[1]);
		// 取索引为length-k的数组值即可
		return nums[k-1];
	}

	/** 插入排序 */
	public void insertionSort(int[] nums) {
		for(int i=0; i<nums.length;i++) {
			for(int j=i;j>0;j--) {
				if (nums[j] < nums[j-1]) {
					int temp = nums[j-1];
					nums[j-1] = nums[j];
					nums[j] = temp;
				}
			}
		}
	}

	public static void main(String[] args) {
		KthLargestNumberInArray kthLargestNumberInArray = new KthLargestNumberInArray();
		int[] nums = new int []{2,3,1,7,9,5,6};
		System.out.println(kthLargestNumberInArray.findKthLargest(nums, 4));
	}
}
